package com.grj.service.redis.controller;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.grj.service.redis.entity.Kills;
import com.grj.service.redis.entity.Orders;
import com.grj.service.redis.entity.User;
import com.grj.service.redis.entity.Voucher;
import com.grj.service.redis.service.KillsService;
import com.grj.service.redis.service.OrdersService;
import com.grj.service.redis.service.VoucherService;
import com.grj.service.redis.service.impl.RedisIdWorker;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * @author 14505
 */
@RestController
@RequestMapping("/kill")
@RequiredArgsConstructor
public class KillController {
    private final KillsService killsService;
    private final VoucherService voucherService;
    private final OrdersService ordersService;
    private final HttpServletResponse httpServletResponse;
    private final HttpServletRequest httpServletRequest;
    @Resource
    private RedisIdWorker redisIdWorker;

    @PostMapping("add")
    public void kill() {
        Voucher voucher = new Voucher();
        voucher.setVoucherId(1L);
        voucher.setTitle("eren");
        voucher.setPayValuce(8000L);
        voucher.setActualValue(100000L);
        voucher.setType(1);
        voucher.setStatus(1);
        voucher.setRule("ok了");
        voucher.setStock(100);
        voucher.setBeginTime(new Date());
        voucher.setEndTime(new Date(System.currentTimeMillis() + 100000000000L));
        voucherService.save(voucher);
        Kills kills = new Kills();
        kills.setStock(voucher.getStock());
        kills.setBeginTime(voucher.getBeginTime());
        kills.setEndTime(voucher.getEndTime());
        kills.setVoucherId(voucher.getVoucherId());
        killsService.save(kills);
    }

    @GetMapping("{id}/{i}")
    public Boolean kill2(@PathVariable Long id, @PathVariable Long i) {
        return killsService.kill(id, i);
    }

//    @Transactional(rollbackFor = Exception.class)
//    void buy(Long vid) {
//        long count = ordersService.count(new LambdaQueryWrapper<Orders>().eq(Orders::getVid, vid).eq(Orders::getUid, 1L));
//        if(count > 0){
//            System.out.println("已经购买过");
//            return;
//        }
//
//        boolean update = killsService.update(new LambdaUpdateWrapper<Kills>().setSql("stock = stock -1").gt(Kills::getStock, 0).eq(Kills::getVoucherId, vid));
//        if(!update){
//            System.out.println("库存不足");
//            return;
//        }
//        Orders orders = new Orders();
//        orders.setId(redisIdWorker.nextId("order"));
//        orders.setUid(Long.valueOf(httpServletResponse.getHeader("user")));
//        orders.setVid(vid);
//        ordersService.save(orders);
//        System.out.println("成功");
//    }


    @GetMapping("token/{id}")
    public void buy(@PathVariable Integer id) {
        System.out.println(id);
    }
}
